<template>
  <el-dialog title="添加人员" :visible.sync="isAddPersonnel" width="40%" :before-close="handleClose" :close-on-click-modal='false'>
    <el-form ref="form" :model="form" label-width="80px" label-position="top" size="small" :rules="rules">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="姓名：" prop="full_name">
            <el-input v-model="form.full_name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别：" prop="sex">
            <el-select v-model="form.sex" placeholder="请选择性别">
              <el-option label="男" value="1"></el-option>
              <el-option label="女" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="账号：" prop="users_name">
            <el-input v-model="form.users_name" placeholder="账号唯一，建议使用姓名字母+身份证后四位。"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="密码：">
            <el-col :span="18" :xl="{span: 20}">
              <el-input type="password" auto-complete="new-password" v-model="form.password" placeholder="初始密码@Mindone123456"></el-input>
            </el-col>
            <el-col :span="3" :xl="{span: 4}">
              <el-button type="text" @click="resetPassWord">重置密码</el-button>
            </el-col>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="手机号：" prop="mobile">
            <el-input v-model="form.mobile"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="角色：" prop="role_id">
            <el-select v-model="form.role_id" placeholder="请选择角色">
              <el-option v-for="roleOptions in this.roleOptions" :key="roleOptions.id" :label="roleOptions.role_name" :value="roleOptions.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="状态：" prop="status">
            <el-select v-model="form.status" placeholder="请选择状态">
              <el-option v-for="statusOptions in this.statusOptions" :key="statusOptions.id" :label="statusOptions.value" :value="statusOptions.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close()">取 消</el-button>
      <el-button type="primary" @click="checkFn()">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import {APIROLELIST, APIMEMBERADDINFO} from '@/common/api.js'
export default {
  props: ['isAddPersonnel'],
  data () {
    return {
      form: {
        password: ''
      },
      rules: {
        full_name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        users_name: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'blur' }
        ],
        role_id: [
          { required: true, message: '请选择角色', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'blur' }
        ]
      },
      mineStatus: '',
      options: [],
      roleOptions: [],
      statusOptions: [
        {id: 1, value: '启用'},
        {id: 0, value: '禁用'}
      ]
    }
  },
  created () {
    this.roleList()
  },
  methods: {
    roleList () {
      APIROLELIST().then((res) => {
        this.roleOptions = res.data.list
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    },
    handleClose () {
      this.$emit('noAddPersonnel')
    },
    close (type) {
      this.$emit('noAddPersonnel', type)
    },
    resetPassWord () {
      this.form.password = '@Mindone123456'
    },
    checkFn () {
      if (this.form.full_name === '') {
        this.$message.warning('姓名不能为空')
        return false
      }
      if (this.form.sex === '') {
        this.$message.warning('性别不能为空')
        return false
      }
      if (this.form.users_name === '') {
        this.$message.warning('账号不能为空')
        return false
      }
      this.submit()
    },
    submit () { // 提交
      APIMEMBERADDINFO({
        full_name: this.form.full_name,
        username: this.form.users_name,
        password: this.form.password,
        mobile: this.form.mobile,
        role_id: this.form.role_id,
        sex: this.form.sex,
        status: this.form.status
      }).then((res) => {
        if (res.status === 1) {
          this.$message.success(res.msg)
          this.close(1)
        } else {
          this.$message.error(res.msg)
        }
      }).catch((err) => {
        this.$message.error(err.msg)
      })
    }
  }
}
</script>
